#include <xdevice/xdevice.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print_usage(const char* prog_name) {
    printf("Usage: %s <command> [args...]\n", prog_name);
    printf("Commands:\n");
    printf("  create-device --name NAME --size SIZE [--type TYPE]\n");
    printf("    创建新设备\n");
    printf("  list-devices\n");
    printf("    列出所有设备\n");
    printf("  get-device-info --name NAME\n");
    printf("    获取设备信息\n");
    printf("  get-cluster-info\n");
    printf("    获取集群信息\n");
}

int cmd_create_device(int argc, char* argv[]) {
    const char* name = NULL;
    uint64_t size = 0;
    const char* type_str = "local";  // 默认类型
    
    for (int i = 2; i < argc; i++) {
        if (strcmp(argv[i], "--name") == 0 && i + 1 < argc) {
            name = argv[++i];
        } else if (strcmp(argv[i], "--size") == 0 && i + 1 < argc) {
            size = strtoull(argv[++i], NULL, 10);
        } else if (strcmp(argv[i], "--type") == 0 && i + 1 < argc) {
            type_str = argv[++i];
            // 验证存储类型
            if (strcmp(type_str, "local") != 0 && 
                strcmp(type_str, "nfs") != 0 && 
                strcmp(type_str, "nvmeof") != 0) {
                fprintf(stderr, "Unknown storage type: %s\n", type_str);
                return 1;
            }
        }
    }
    
    if (!name || size == 0) {
        fprintf(stderr, "Error: --name and --size are required\n");
        return 1;
    }
    
    printf("Creating device '%s' with size %lu bytes (type: %s)...\n", name, size, type_str);
    
    // 这里只是演示，实际需要连接到运行的xdevice节点
    printf("Device created successfully!\n");
    return 0;
}

int cmd_list_devices(void) {
    printf("Listing devices...\n");
    printf("(No devices found - this is a stub implementation)\n");
    return 0;
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        print_usage(argv[0]);
        return 1;
    }
    
    const char* command = argv[1];
    
    if (strcmp(command, "create-device") == 0) {
        return cmd_create_device(argc, argv);
    } else if (strcmp(command, "list-devices") == 0) {
        return cmd_list_devices();
    } else if (strcmp(command, "get-device-info") == 0) {
        printf("get-device-info: Not implemented\n");
        return 1;
    } else if (strcmp(command, "get-cluster-info") == 0) {
        printf("get-cluster-info: Not implemented\n");
        return 1;
    } else {
        fprintf(stderr, "Unknown command: %s\n", command);
        print_usage(argv[0]);
        return 1;
    }
}
